﻿
using System.Collections.Generic;
using System.Data;
using System;
namespace ZHDJ.MobileInterface
{
    public partial class UserRegister : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            UserRegisterOutParam result = new UserRegisterOutParam();
            UserRegisterInParam param = new UserRegisterInParam();
            string logMsg = "";
            bool isFlag = true;

            try
            {
                param = inParam as UserRegisterInParam;
                //if (string.IsNullOrEmpty(param.user_name) || string.IsNullOrEmpty(param.cname) || string.IsNullOrEmpty(param.mobile)
                //    || string.IsNullOrEmpty(param.vcode) || string.IsNullOrEmpty(param.idcard))
                if (string.IsNullOrEmpty(param.user_name) || string.IsNullOrEmpty(param.cname)
                    || string.IsNullOrEmpty(param.mobile) || string.IsNullOrEmpty(param.idcard))
                {
                    result.code = "0";
                    result.desc = "用户名，手机号码，身份证号码，以及姓名不能为空";

                    isFlag = false;
                }

                if (isFlag)
                {
                    if (string.IsNullOrEmpty(param.pwd) || param.pwd.Length < 6 || param.pwd.Length > 16)
                    {
                        result.code = "0";
                        result.desc = "密码必须为6位至16位英文和数字组合";

                        isFlag = false;
                    }
                    param.pwd = param.pwd.ToLower();
                }

                string url = CommonUtils.AppAddressNote + "WS/PartySmsCheckService.asmx";
                PartySmsCheckService service = new PartySmsCheckService(url);
                if (isFlag)
                {
                    if (!service.CheckMobileUnique(param.mobile))
                    {
                        result.code = "0";
                        result.desc = "你输入的手机号码：" + param.mobile + " 已经存在，请更改后重新注册！";

                        isFlag = false;
                    }
                }


                if (isFlag)
                {
                    param.idcard = param.idcard.Trim();
                    if (InterfaceHelper.CheckIdentityCardIsExists(param.idcard))
                    {
                        result.code = "0";
                        result.desc = "你输入的身份证号码：" + param.idcard + " 已经存在，请更改后重新注册！";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    param.user_name = param.user_name.Trim();
                    param.mobile = param.mobile.Trim();
                    if (InterfaceHelper.CheckLoginIDCount(param.user_name))
                    {
                        result.code = "0";
                        result.desc = "你输入的用户名：" + param.user_name + " 已经存在，请更改后重新注册！";

                        isFlag = false;
                    }
                    if (InterfaceHelper.CheckLoginIDCountEx(param.user_name))
                    {
                        result.code = "0";
                        result.desc = "你输入的用户名：" + param.user_name + " 已经存在，请更改后重新注册！";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    param.pwd = CommonUtils.GetMD5(param.pwd);
                    InterfaceHelper.InsertMembers(param);

                    result.data.user_id = param.user_name;
                }
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode + "-UserRegister", SerializeHelper.JsonSerializer<UserRegisterInParam>(param), logMsg);
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<UserRegisterInParam, UserRegisterOutParam>();
            else
                SafeProcessRequest<UserRegisterInParam, UserRegisterOutParam>();
        }
    }

    #region 请求参数
    public class UserRegisterInParam
    {
        /// <summary>
        ///	用户名	必须
        /// </summary>
        public string user_name
        { get; set; }
        /// <summary>
        ///	密码	 登录时的密码，6位至16位，英文和数字组合，暂不区分大小写
        /// </summary>
        public string pwd
        { get; set; }
        /// <summary>
        ///	身份证号	
        /// </summary>
        public string idcard
        { get; set; }
        /// <summary>
        ///	验证码	
        /// </summary>
        //public string vcode
        //{ get; set; }
        /// <summary>
        ///	姓名	 用户真实姓名，中文汉字（必填）
        /// </summary>
        public string cname
        { get; set; }
        /// <summary>
        ///	手机号码	
        /// </summary>
        public string mobile
        { get; set; }
        /// <summary>
        ///	用户类型	
        /// </summary>
        public string UserType
        { get; set; }
        /// <summary>
        ///	工作地址	
        /// </summary>
        public string WorkAddress
        { get; set; }
        /// <summary>
        ///	户籍地址	
        /// </summary>
        public string CensusAddress
        { get; set; }

        /// <summary>
        ///	邮箱	
        /// </summary>
        public string Email
        { get; set; }

        /// <summary>
        ///	工作单位	
        /// </summary>
        public string WorkUnit
        { get; set; }

        /// <summary>
        ///	单位职务	
        /// </summary>
        public string UnitJob
        { get; set; }
    }
    #endregion

    #region 结果参数
    public class UserRegisterOutParam
    {
        public UserRegisterOutParam()
        {
            code = "1";
            desc = "操作成功";

            data = new UserRegisterOutData();
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }

        public UserRegisterOutData data
        { get; set; }
    }

    public class UserRegisterOutData
    {
        /// <summary>
        ///	用户ID 
        /// </summary>
        public string user_id
        { get; set; }
    }
    #endregion
}